from zoneapi.models import ZoneInfo
from django.shortcuts import render

# Create your views here.
from django.http import JsonResponse
from django.db import connection
import json,datetime,requests
from dateutil.relativedelta import relativedelta
from rest_framework.decorators import api_view
from buildapi.models import BuildingInfo
from guestapi.models import GuestInfo
from houseapi.models import HouseInfo
# Create your views here.


@api_view(['POST']) 
def dispatcher(request):  

    # try:
    if request.user.is_authenticated:   # 验证Token是否正确
        if request.user.is_active:
            # POST/PUT/DELETE 请求 参数 从 request 对象的 body 属性中获取
            if request.method in ['POST','PUT','DELETE']:
                # 根据接口，POST/PUT/DELETE 请求的消息体都是 json格式
                request.params = json.loads(request.body)


            # 根据不同的action分派给不同的函数进行处理
            action = request.params['action']
            if action == 'list':
                return GuestInfo_list(request)
            elif action == 'add':
                return GuestInfo_add(request)
            elif action == 're':  
                return GuestInfo_re(request)
            elif action == 'del':
                return GuestInfo_del(request)
            elif action == 'like':
                return GuestInfo_like(request)
            elif action == 'include':
                return GuestInfo_include(request)
            elif action == 'set':
                return GuestInfo_set(request)
            elif action == 'price':
                return GuestInfo_price(request)
            elif action == 'deposit':
                return GuestInfo_deposit(request)

            else:
                return JsonResponse({'ret': 1, 'msg': '不支持该类型http请求'})
    else:
        return JsonResponse({'ret': 2, "msg": "token验证失败"})


def GuestInfo_deposit(request):
    data = json.loads(request.body)  
    try:
        guest_unionid = data['guest_unionid']
        deposit_sum = data['deposit_sum']
        if GuestInfo.objects.filter(guest_unionid=guest_unionid).exists():
            re = GuestInfo.objects.get(guest_unionid=guest_unionid)
            re.guest_balance = re.guest_balance + deposit_sum
            re.save()

        return JsonResponse({'ret':0})
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
                'ret': 1,
                'msg': "充值失败，发生异常:{}".format(e)
            })

def GuestInfo_price(request):
    data = json.loads(request.body)
    guest_id = data['guest_id']
    type_list = data['price_type']
    try:
        for price_type in type_list:
            if GuestInfo.objects.filter(guest_id=guest_id).exists():
                index = GuestInfo.objects.filter(guest_id=guest_id).values()
                guest_index = index[0]

                if HouseInfo.objects.filter(house_guest_id=guest_id).exists():
                    house = HouseInfo.objects.filter(house_guest_id=guest_id).values()
                    house_index = list(house)[0]
                else:
                    return JsonResponse({'ret':2,'msg':'未找到对应房间'})
                #联查楼栋信息 
                build = BuildingInfo.objects.filter(building_id=house_index['house_about_id']).values()
                build_index = list(build)[0]
                
                bre = BuildingInfo.objects.get(building_id=build_index['building_id'])
                re = GuestInfo.objects.get(guest_id=guest_id)
                #电费
                if price_type == 'electric':
                    bre.building_price = bre.building_price - guest_index['guest_electric_price']
                    re.guest_electric_price_type = 1
                #水费
                elif price_type == 'water':
                    bre.building_price = bre.building_price - guest_index['guest_water_price']
                    re.guest_water_price_type = 1
                #房租
                elif price_type == 'house':
                    bre.building_price = bre.building_price - guest_index['guest_price']
                    re.guest_price_type = 1
                #网费
                elif price_type == 'network':
                    now_time = datetime.datetime.strptime(datetime.datetime.now().strftime(f'%Y-%m-%d'),f'%Y-%m-%d')
                    bre.building_price = bre.building_price - guest_index['guest_network_price']
                    re.guest_network_price_type = 1
                    re.guest_network_date = now_time

                bre.save()
                re.save()
                
                if re.guest_network_price_type == 1:
                    from_data = {
                                    "building_id": build_index['building_id'],
                                    "uid" : re.guest_network_id,
                                    "status" : "up"
                                }

                    from_header = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
                    requests.post('http://120.79.87.40:8000/ikuaiapi/enabled', data=json.dumps(from_data), headers=from_header)

        return JsonResponse({'ret':0})
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
                'ret': 1,
                'msg': "操作失败，发生异常:{}".format(e)
            })


def GuestInfo_list(request):
    data = json.loads(request.body)  
    openid = data['openid']
    page_max = int(data['page_max'])
    page = int(data['page'])
    try:
        index = GuestInfo.objects.filter(guest_openid=openid).values('guest_id','guest_name','guest_nbr','guest_phone','guest_date1','guest_date2').order_by('guest_id')[page_max*page-page_max:page_max*page]
        count = GuestInfo.objects.filter(guest_openid=openid).count()

        return JsonResponse({'ret':0,'data':list(index),'count':count})
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
                'ret': 1,
                'msg': "列表获取失败，发生异常:{}".format(e)
            })


def GuestInfo_add(request):
    data = json.loads(request.body)  
    try:
        guest_name = data['guest_name']
        guest_date1 = data['guest_date1']
        guest_date2 = data['guest_date2']
        guest_nbr = data['guest_nbr']
        guest_phone =data['guest_phone']
        guest_openid = data['guest_openid']
        
        GuestInfo.objects.create(
        guest_name = guest_name,
        guest_openid = guest_openid,
        guest_date1 = guest_date1,
        guest_date2 = guest_date2,
        guest_nbr = guest_nbr,
        guest_phone = guest_phone,
        guest_network_price_type=2,
        guest_electric_price = 0,
        guest_water_price = 0,
        guest_price_type = 0,
        guest_electric_price_type = 0,
        guest_water_price_type = 0,
        guest_balance = 0,
        guest_network_price = 0,
        guest_price = 0)

        return JsonResponse({'ret':0})

    except Exception as e:
        return JsonResponse({
                'ret': 1,
                'msg': "信息添加失败，发生异常:{}".format(e)
            })


def GuestInfo_re(request):
    data = json.loads(request.body)  
    cursor = connection.cursor()
    try:
        key = list(data['key'])
        value = list(data['value'])
        guest_id = data['guest_id']

        if len(key) > 0 :
            for i in range(len(key)):
                
                cursor.execute(" UPDATE guest_info SET {} = '{}' WHERE guest_id = '{}' ".format(key[i],value[i],guest_id))

        return JsonResponse({'ret':0})
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
                'ret': 1,
                'msg': "信息修改失败，发生异常:{}".format(e)
            })

def GuestInfo_del(request):
    data = json.loads(request.body)
    try:
        guest_id = list(data['guest_id'])

        if len(guest_id) > 0 :
            for i in range(len(guest_id)):

                index = GuestInfo.objects.get(guest_id=guest_id[i])
                if HouseInfo.objects.filter(house_guest_id=guest_id[i]).exists():
                    house = HouseInfo.objects.filter(house_guest_id=guest_id[i]).values()
                    house_index = list(house)[0]

                    re = HouseInfo.objects.get(house_id=house_index['house_id'])
                    re.house_date1 = None
                    re.house_date2 = None
                    re.house_type = 0
                    re.house_guest_id = None
                    re.house_guest_name = None
                    re.save()

                index.delete()
    except Exception as e:
        return JsonResponse({
            'ret': 1,
            'msg': "信息删除失败，发生异常：{}".format(e)
        })
    return JsonResponse({'ret':0})

def GuestInfo_like(request):
    data = json.loads(request.body)
    openid = data['openid']
    try:
        guest_name = data['guest_name']
        index = GuestInfo.objects.filter(guest_name__icontains=guest_name,guest_openid=openid).values('guest_id','guest_name','guest_nbr','guest_phone','guest_date1','guest_date2')
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
            'ret': 1,
            'msg': "查询失败，发生异常：{}".format(e)
        })
    return JsonResponse({'ret':0,'data':list(index)})


def GuestInfo_include(request):
    data = json.loads(request.body)
    data_type = data['type']
    try:
        if data_type == 'guest_id':
            guest_id = data['guest_id']
            index = GuestInfo.objects.filter(guest_id=guest_id).values()
        elif data_type == 'unionid':
            guest_unionid = data['guest_unionid']
            index = GuestInfo.objects.filter(guest_unionid=guest_unionid).values()
    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
            'ret': 1,
            'msg': "查询失败，发生异常：{}".format(e)
        })
    return JsonResponse({'ret':0,'data':list(index)})


def GuestInfo_set(request):
    data = json.loads(request.body)
    try:
        set_type = data['set_type']

        if set_type == 'guest':
            guest_id = data['guest_id']
            guest_start_date = data['guest_start_date']
            #下一个扣费时间
            start_time = datetime.datetime.strptime(guest_start_date,f'%Y-%m-%d')
            end_time = start_time + relativedelta(months=+1)
            re = GuestInfo.objects.get(guest_id=guest_id)
            re.guest_start_date = end_time
            re.save()
        if set_type == 'house':
            house_id = data['house_id']
            house_start_time = data['house_start_time']
            #下一个扣费时间
            start_time = datetime.datetime.strptime(house_start_time,f'%Y-%m-%d')
            end_time = start_time + relativedelta(months=+1)
            re = HouseInfo.objects.get(house_id=house_id)
            re.house_start_time = end_time
            re.save()

    except GuestInfo.DoesNotExist as e:
        return JsonResponse({
            'ret': 1,
            'msg': "设置失败，发生异常：{}".format(e)
        })
    return JsonResponse({'ret':0})


@api_view(['GET'])
def GuestInfo_clean(request):
    try:
        count = BuildingInfo.objects.values('building_id').last()['building_id']
        for i in range(count):
            i = i+1
            if i > count:
                break
            
            if BuildingInfo.objects.filter(building_id=i).exists():
                bre = BuildingInfo.objects.get(building_id=i)
                #清零
                bre.building_price_last = bre.building_price
                bre.building_electric_last = bre.building_electric
                bre.building_water_last = bre.building_water
                bre.building_price = 0
                bre.building_electric = 0
                bre.building_water = 0
                bre.save()

            if ZoneInfo.objects.filter(zone_id=i).exists():
                zre = ZoneInfo.objects.get(zone_id=i)
                zre.zone_last_electric = zre.zone_electric
                zre.zone_last_water = zre.zone_water
                zre.zone_last_house = zre.zone_house
                zre.zone_electric = 0
                zre.zone_water = 0
                zre.zone_house = 0
                zre.save()
            else:
                pass

    except Exception as e:
        return JsonResponse({
            'ret': 1,
            'msg': "发生异常：{}".format(e)
        })
    return JsonResponse({'ret':0})


@api_view(['GET'])
def GuestInfo_compute(request):
    #当前时间
    now_time = datetime.datetime.strptime(datetime.datetime.now().strftime(f'%Y-%m-%d'),f'%Y-%m-%d')
    count = GuestInfo.objects.values('guest_id').last()['guest_id']

    #登陆水电平台
    token = {"Authorization" : "Basic b3BlbmNsaWVudDpvcGVuY2xpZW50"}

    login = requests.post('http://www.meter-iot.cn/api/auth/oauth/token?grant_type=client_credentials&scope=open', headers=token)
    login_index = login.json()
    login_token = {"Authorization" : "Bearer {}".format(login_index['access_token'])}

    for i in range(count):
        i = i+1
        if i > count:
            break
        
        if GuestInfo.objects.filter(guest_id=i).exists():
            re = GuestInfo.objects.get(guest_id=i)

            if re.guest_network_price_type != 2 and re.guest_network_date is not None :
                #获取网络计费时间
                network_time = datetime.datetime.strptime(re.guest_network_date,f'%Y-%m-%d %H:%M:%S')
                
                #开始计费
                if network_time + relativedelta(months=+1) <= now_time:
                    network_day = now_time - network_time
                    house = HouseInfo.objects.filter(house_id=i).values()
                    house_list = list(house)[0]
                    build = BuildingInfo.objects.filter(building_id=house_list['house_about_id']).values()
                    build_index = list(build)[0]

                    if build_index['building_network_tariff'] is not None:
                        bre = BuildingInfo.objects.get(building_id=build_index['building_id'])
                        network_price = build_index['building_network_tariff'] * network_day.days
                        if re.guest_balance >= network_price:
                            re.guest_balance = re.guest_balance - network_price
                            re.guest_network_day = network_day.days
                            re.guest_network_date = network_time + relativedelta(months=+1)
                            re.guest_network_price = network_price
                            re.guest_network_price_type = 1
                            re.save()
                        elif re.guest_network_price_type == 3:
                            re.guest_network_day = network_day.days
                            re.guest_network_price = network_price
                            re.save()
                        else:
                            from_data = {
                                "building_id": build_index['building_id'],
                                "uid" : re.guest_network_id,
                                "status" : "down"
                            }
                            
                            from_header = {"Content-Type": "application/json; charset=UTF-8", 'Connection': 'close'}
                            requests.post('http://120.79.87.40:8000/ikuaiapi/enabled', data=json.dumps(from_data), headers=from_header)
                            re.guest_network_price_type = 3
                            re.guest_network_day = network_day.days
                            re.guest_network_price = network_price
                            re.save()
                        
                        bre.building_price = bre.building_price + (build_index['building_network_tariff'] * network_day.days)
                        bre.save()

        #轮询用户信息
        if HouseInfo.objects.filter(house_guest_id=i,house_mode=0).exists():
            #为空判断
            if GuestInfo.objects.filter(guest_id=i).exists():
                guest_index = GuestInfo.objects.get(guest_id=i)
            else:
                continue

            if guest_index.guest_start_date is not None:
                start_time = datetime.datetime.strptime(guest_index.guest_start_date,f'%Y-%m-%d %H:%M:%S')
            else:
                continue

            if start_time is not None:
                if now_time >= start_time - relativedelta(days=+5):
                    #获取token
                    access = requests.get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx251bfb5939f895d1&secret=a5683202dc42889372e0ed983244867a')
                    access_index = access.json()
                    access_token = access_index['access_token']

                    wx_data = {
                        "touser": "{}".format(guest_index.guest_user_openid),
                        "template_id": "IKHkWR9b1n6PwD5dbKaJTuruPDTsFCF6OC0Be_7BGe4",
                        "url": f"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx251bfb5939f895d1&redirect_uri=https%3A%2F%2Fphone.liandunkaoqin.com%2Flogin&response_type=code&scope=snsapi_userinfo#wechat_redirect",
                        "topcolor": "#FF0000",
                        "data": {
                            "first": {
                                "value": "您好，您的月度结算账单如下",
                                "color": "#173177"
                            },
                            "keyword1": {
                                "value": "{}年{}月{}日".format(datetime.datetime.now().year,datetime.datetime.now().month,datetime.datetime.now().day),
                                "color": "#173177"
                            },
                            "keyword2": {
                                "value": "水费：{} | 电费：{}".format(guest_index.guest_water_price,guest_index.guest_electric_price),
                                "color": "#173177"
                            },
                            "keyword3": {
                                "value": "房屋租金：{} | 网络费：{}".format(guest_index.guest_price,guest_index.guest_network_price),
                                "color": "#173177"
                            },
                            "keyword4": {
                                "value": "{}元".format(guest_index.guest_water_price + guest_index.guest_electric_price + guest_index.guest_price + guest_index.guest_network_price),
                                "color": "#173177"
                            },
                            "remark": {
                                "value": "您余额为：{} | 若您的余额不足，可点击跳转至住户端充值缴纳。".format(guest_index.guest_balance),
                                "color": "#173177"
                            }
                        }
                    }

                    requests.post('https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(access_token), data=json.dumps(wx_data))

                if now_time >= start_time:
                    #开始计费流程
                    house = HouseInfo.objects.filter(house_guest_id=i).values()
                    house_list = list(house)
                    #声明变量
                    electric_price = 0.0
                    water_price = 0.0
                    house_price = 0.0

                    for x in range(len(house_list)):
                        if x > len(house_list):
                            break
                        
                        #获取房间信息
                        house_index = house_list[x]
                        #联查楼栋信息
                        build = BuildingInfo.objects.get(building_id=house_index['house_about_id'])
                        hre = HouseInfo.objects.get(house_id=house_index['house_id'])
                        building_electric_price = build.building_electric_price
                        building_water_price = build.building_water_price
                        #查询对应房间电表
                        electric = requests.get('http://www.meter-iot.cn/api/open/openapi/read/info/47c179f7b2e04f0f9d83539b084012a4/{}'.format(house_index['house_electric']), headers=login_token)
                        electric_index = electric.json()
                        if 'code' not in electric_index:
                            hre.house_electric_valve = 3
                            hre.save()
                        elif electric_index['code'] == 1:
                            hre.house_electric_valve = 3
                            hre.save()
                        else:
                            electric_extendDict = electric_index['data']['extendDict']['deviceTotalData'] / 1000
                        
                        #查询对应房间水表
                        water = requests.get('http://www.meter-iot.cn/api/open/openapi/read/info/47c179f7b2e04f0f9d83539b084012a4/{}'.format(house_index['house_water']), headers=login_token)
                        water_index = water.json()
                        if 'code' not in water_index:
                            hre.house_water_valve = 3
                            hre.save()
                        if water_index['code'] == 1:
                            hre.house_water_valve = 3
                            hre.save()
                        else:
                            water_extendDict = water_index['data']['extendDict']['deviceTotalData'] / 1000

                        #下一个计费时间
                        end_time = start_time + relativedelta(months=+1)

                        bre = BuildingInfo.objects.get(building_id=build.building_id)

                        if electric_extendDict is not None:
                            #电费
                            now_electric_price = (electric_extendDict - hre.house_electric_last) * building_electric_price
                            electric_price += now_electric_price

                            hre.house_electric_last = electric_extendDict

                            bre.building_price = bre.building_price + now_electric_price
                            bre.building_electric = bre.building_electric + electric_extendDict

                        if water_extendDict is not None:
                            #水费
                            now_water_price = (water_extendDict - hre.house_water_last) * building_water_price
                            water_price += now_water_price

                            hre.house_water_last = water_extendDict

                            bre.building_price = bre.building_price + now_water_price
                            bre.building_water = bre.building_water + water_extendDict

                        #房租
                        house_price += house_index['house_price']

                        bre.building_price = bre.building_price + house_index['house_price']

                        hre.save()
                        bre.save()
                    
                    #总账
                    re = GuestInfo.objects.get(guest_id=house_list[0]['house_guest_id'])

                    #电费总
                    if re.guest_balance >= electric_price:
                        re.guest_balance = re.guest_balance - electric_price
                        re.guest_last_electric_price = re.guest_electric_price
                        re.guest_electric_price = electric_price
                        re.guest_electric_price_type = 1
                        re.save()

                        if hre.house_electric_valve == 2:
                            #开闸
                            valve = requests.post('http://www.meter-iot.cn/api/open/openapi/valve/47c179f7b2e04f0f9d83539b084012a4/{}?valveStatus={}'.format(house_index['house_electric'],1), headers=login_token)
                            tool_valve_index = valve.json()
                            if 'code' not in tool_valve_index:
                                hre.house_electric_valve = 3
                                hre.save()
                            elif tool_valve_index['code'] == 1:
                                hre.house_electric_valve = 3
                                hre.save()
                            else:
                                hre.house_electric_valve = 1
                                hre.save()
                    else:
                        #关闸
                        re.guest_electric_price = re.guest_electric_price + electric_price
                        re.guest_electric_price_type = 0
                        valve = requests.post('http://www.meter-iot.cn/api/open/openapi/valve/47c179f7b2e04f0f9d83539b084012a4/{}?valveStatus={}'.format(house_index['house_electric'],2), headers=login_token)
                        tool_valve_index = valve.json()
                        if 'code' not in tool_valve_index:
                            hre.house_electric_valve = 3
                            hre.save()
                        elif tool_valve_index['code'] == 1:
                            hre.house_electric_valve = 3
                            hre.save()
                        else:
                            hre.house_electric_valve = 2
                            hre.save()

                    #水费总
                    if re.guest_balance >= water_price:
                        re.guest_balance = re.guest_balance - water_price
                        re.guest_last_water_price = re.guest_water_price
                        re.guest_water_price = water_price
                        re.guest_water_price_type = 1
                        re.save()
                        
                        #开闸
                        if hre.house_water_valve == 2:
                            valve = requests.post('http://www.meter-iot.cn/api/open/openapi/valve/47c179f7b2e04f0f9d83539b084012a4/{}?valveStatus={}'.format(house_index['house_water'],1), headers=login_token)
                            tool_valve_index = valve.json()
                            if 'code' not in tool_valve_index:
                                hre.house_water_valve = 3
                                hre.save()
                            elif tool_valve_index['code'] == 1:
                                hre.house_water_valve = 3
                                hre.save()
                            else:
                                hre.house_water_valve = 1
                                hre.save()
                    else:
                        #关闸
                        re.guest_water_price = re.guest_water_price + water_price
                        re.guest_water_price_type = 0
                        valve = requests.post('http://www.meter-iot.cn/api/open/openapi/valve/47c179f7b2e04f0f9d83539b084012a4/{}?valveStatus={}'.format(house_index['house_water'],2), headers=login_token)
                        tool_valve_index = valve.json()
                        if 'code' not in tool_valve_index:
                            hre.house_water_valve = 3
                            hre.save()
                        elif tool_valve_index['code'] == 1:
                            hre.house_water_valve = 3
                            hre.save()
                        else:
                            hre.house_water_valve = 2
                            hre.save()
                    
                    #房租总
                    if re.guest_balance >= re.guest_price:
                        re.guest_start_date = end_time
                        re.guest_balance = re.guest_balance - re.guest_price
                        re.guest_last_price = re.guest_price
                        re.guest_price = house_price
                        re.guest_price_type = 1
                        re.save()
                    else:
                        re.guest_price = re.guest_price + house_price
                        re.guest_price_type = 0
                        re.save()

    count = HouseInfo.objects.values('house_id').last()['house_id']

    for i in range(count):
        i = i+1
        if i > count:
            break

        if HouseInfo.objects.filter(house_id=i,house_mode=1).exists():
            re = HouseInfo.objects.get(house_id=i)
            bre = BuildingInfo.objects.get(building_id=re.house_about_id)
            if re.house_start_time is not None:
                start_time = datetime.datetime.strptime(re.house_start_time,f'%Y-%m-%d %H:%M:%S')
                if now_time >= start_time:
                    #电
                    electric = requests.get('http://www.meter-iot.cn/api/open/openapi/read/info/47c179f7b2e04f0f9d83539b084012a4/{}'.format(re.house_electric), headers=login_token)
                    electric_index = electric.json()
                    if 'code' not in electric_index:
                        re.house_electric_valve = 3
                        re.save()
                    elif electric_index['code'] == 1:
                        re.house_electric_valve = 3
                        re.save()
                    else:
                        electric_extendDict = electric_index['data']['extendDict']['deviceTotalData'] /1000
                    #水
                    water = requests.get('http://www.meter-iot.cn/api/open/openapi/read/info/47c179f7b2e04f0f9d83539b084012a4/{}'.format(re.house_water), headers=login_token)
                    water_index = water.json()
                    if 'code' not in water_index:
                        re.house_water_valve = 3
                        re.save()
                    elif water_index['code'] == 1:
                        re.house_water_valve = 3
                        re.save()
                    else:
                        water_extendDict = water_index['data']['extendDict']['deviceTotalData'] /1000
                    #计费
                    end_time = start_time + relativedelta(months=+1)
                    if electric_extendDict is not None:
                        #电费
                        now_electric_price = (electric_extendDict - re.house_electric_last) * bre.building_electric_price

                        re.house_electric_last = electric_extendDict
                        re.house_electric_price_last = re.house_electric_price
                        re.house_electric_price = now_electric_price
                        if re.house_electric_valve == 3:
                            re.house_electric_valve = 1

                        bre.building_price = bre.building_price + now_electric_price
                        bre.building_electric = bre.building_electric + electric_extendDict

                    if water_extendDict is not None:
                        #水费
                        now_water_price = (water_extendDict - re.house_water_last) * bre.building_water_price

                        re.house_water_last = water_extendDict
                        re.house_water_price_last = re.house_water_price
                        re.house_water_price = now_water_price
                        if re.house_water_valve == 3:
                            re.house_water_valve = 1

                        bre.building_price = bre.building_price + now_water_price
                        bre.building_water = bre.building_water + water_extendDict
                    
                    bre.building_price = bre.building_price + re.house_price
                    re.house_start_time = end_time
                    re.save()
                    bre.save()

    return JsonResponse({'ret':0,'msg':'ok'})


